
#include "app_adc.h"

static uint16_t VREFINT_CAL=0;
/**
 * @brief ADC Initialization Function
 * @param None
 * @retval None
 */
void ADC_Init(void)
{
  analogReadResolution(12); 
  VREFINT_CAL = *(uint16_t *)(VREFINT_CAL_ADDR);
  PRINT_DEBUG("VREFINT_CAL:%d\n", VREFINT_CAL);
  PRINT_DEBUG("VREFINT_CAL_VREF:%d\n", VREFINT_CAL_VREF);
}

/**
 * @brief ADC READ Voltage Function
 * @param ulPin
 * @retval Voltage mV
 */
uint32_t ADC_READ_Voltage(uint32_t ulPin)
{
    uint32_t ADC_value=0;
    uint32_t ADC_VREFINT_value=0;
    uint32_t Voltage=0;
    uint32_t VREFINT=VREFINT_CAL_VREF*VREFINT_CAL/4095;
    // PRINT_DEBUG("ADC VREFINT:%d\n", VREFINT);
    ADC_value=analogRead(ulPin);
    // PRINT_DEBUG("ADC_value:%d\n",ADC_value);
    ADC_VREFINT_value=analogRead(AVREF);
    PRINT_DEBUG("AVREF ADC_value,ADC_value:%d,%d\n", ADC_VREFINT_value,ADC_value); 
    // PRINT_DEBUG("***\n"); 
    Voltage=VREFINT*ADC_value/ADC_VREFINT_value;
        // Voltage=VREFINT_CAL_VREF*VREFINT_CAL*ADC_value/ADC_VREFINT_value/4095*1000;
    // PRINT_DEBUG("Voltage:%dmv\n", Voltage);
    return Voltage;
}

